import { GameConfig } from "../configs/GameConfig";
import { IGrowTypeElement } from "../configs/GrowType";
import { UIPool } from "../etc/UIPool";
import { GrowMapping, GrowModuleC, GrowModuleData } from "../module/GrowModule";
import GrowItem_Generate from "../ui-generate/GrowItem_generate";
import GrowWindow_Generate from "../ui-generate/GrowWindow_generate";

export class GrowWindow extends GrowWindow_Generate{
    private pool:UIPool<GrowItem>
    private md:GrowModuleC
    private mdData:GrowModuleData

    onStart(){
        this.md = ModuleService.getModule(GrowModuleC)
        this.mdData = DataCenterC.getData(GrowModuleData)

        this.btn_close.onClicked.add(()=>{
            UIService.hideUI(this)
        })

        this.pool = new UIPool(()=>{
            let item = UIService.create(GrowItem)
            this.con_items.addChild(item.uiObject)
            return item
        })
    }

    onShow(){
        this.mdData.onDataChange.add(this.onGrowDataChanged,this)
        this.pool.resetAll()
        for (const cfg of GameConfig.GrowType.getAllElement()) {
            let item = this.pool.get()
            item.setInfo(cfg)
        }
    }

    onHide(){
        this.mdData.onDataChange.remove(this.onGrowDataChanged,this)
    }

    private onGrowDataChanged(){
        this.pool.eachVisibleItem(item=>{
            item.refresh()
        })
    }
}

export class GrowItem extends GrowItem_Generate{
    private cfg:IGrowTypeElement

    onStart(){
        this.btn_buy.onClicked.add(this.onClickedBuy.bind(this))
    }

    setInfo(cfg:IGrowTypeElement){
        this.cfg = cfg
        this.refresh()
    }

    refresh(){
        let cfgArray = GrowMapping.getGrowCfgArrayByKind(this.cfg.ID)
        let growData = DataCenterC.getData(GrowModuleData)
        
        this.txt_grow_type.text = this.cfg.Name
        
        let currentProcess:number
        if(!growData.growMap[this.cfg.ID]){
            currentProcess = 0
        }else{
            currentProcess = GameConfig.Grow.getElement(growData.growMap[this.cfg.ID]).Index
        }
        
        this.bar_process.currentValue = currentProcess / cfgArray.length
        this.img_icon.imageGuid = this.cfg.Icon
        // this.img_bg.imageColor = LinearColor.colorHexToLinearColor(this.cfg.Color)

        let nextCfgID:number
        if(!growData.growMap[this.cfg.ID]){
            nextCfgID = GrowMapping.getGrowFirstCfgByKind(this.cfg.ID).ID
        }else{
            let currentCfg = GameConfig.Grow.getElement(growData.growMap[this.cfg.ID])
            nextCfgID = currentCfg.Next
        }

        this.txt_cost.visibility = nextCfgID == 0 ? SlateVisibility.Collapsed : SlateVisibility.SelfHitTestInvisible
        this.btn_buy.visibility = nextCfgID == 0 ? SlateVisibility.Collapsed : SlateVisibility.Visible

        if(nextCfgID != 0){
            let nextCfg = GameConfig.Grow.getElement(nextCfgID)
            this.txt_cost.text = `消耗${nextCfg.CostMoney}金币`
            this.txt_process.text = `(${currentProcess}/${cfgArray.length})`
        }
        // 已满级
        else{
            this.txt_process.text = `已满级`
        }
    }

    onClickedBuy(){
        ModuleService.getModule(GrowModuleC).requestBuyGrow(this.cfg.ID)
    }
}